﻿using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.Linq;
using System.Text;
using System;
using com.guojin.mj.net.message.game;
public class Setnumber : MonoBehaviour {

    public Button Ret, Send, Sex,heads,closePhoto,takephoto,savephoto;
    public Image SexImage;
    public RawImage head;
    public InputField Name;
    private bool Nv=true;
    public int sex = 1;
    private string acct;
    private string pass;
    public GameObject photo,BG_image;
    public string headsBase64="a";
    // Use this for initialization
    void Start () {
        Sex.onClick.AddListener(ChangeSex);
        Send.onClick.AddListener(SendName);
        heads.onClick.AddListener(ShowPhoto);
        closePhoto.onClick.AddListener(ClosePhoto);
        takephoto.onClick.AddListener(TakePhoto);
        savephoto.onClick.AddListener(TakePhotoSave);
        Ret.onClick.AddListener(Close);
        Screen.autorotateToPortrait = true;
        Screen.autorotateToPortraitUpsideDown = true;
        Screen.autorotateToLandscapeRight = false;
        Screen.autorotateToLandscapeLeft = false;
    }
    //设置账号 sc     验证码pa
    public void SetAcc(string sc,string pa) {
        acct = sc;
        pass = pa;
    }
    void ChangeSex() {
        if (Nv)
        {
            sex = 2;
            SexImage.sprite = Resources.Load<Sprite>("UI/性别_女");
            Nv = false;
        }
        else
        {
            sex = 1;
            SexImage.sprite = Resources.Load<Sprite>("UI/性别_男");
            Nv = true;
        }
    }
    void ShowPhoto() {
        photo.SetActive(true);
    }
    void ClosePhoto() {
        photo.SetActive(false);
    }
    void TakePhoto() {
        LoginAndShare.Controller.OpenPhoto(0);
    }
    void TakePhotoSave()
    {
        LoginAndShare.Controller.OpenPhoto(1);
    }
    void Close() {
        Destroy(this.gameObject);
    }
    private string ServerAdress = "http://121.40.90.67:8092/up";
    //这个是收到1.25   4类型代表语音
    public int msgType = 4;

    //发送录音文件字节流，将返回字节流广播出去
    public void SendName()
    {
        if (Name.text == null || Name.text == "")
        {
            TooL.WenXinTiSi("昵称不能为空");
            return;
        }
        if (headsBase64 == "a"|| headsBase64 ==""|| headsBase64 ==null)
        {
            TooL.WenXinTiSi("头像不能为空");
            return;
        }
        string param = "{ \"type\":\"img\",\"data\":\"data:image/png;base64," + headsBase64 + "\"}";
        Dictionary<string, string> dic = new Dictionary<string, string>();
        dic.Add("data", param);
        string result = HttpPostData(ServerAdress, dic);
        Debug.Log("服务器返回地址:" + result);
        //BG_image.SetActive(true);
        MessageManager.GetInstance().Send7148(result, Name.text, sex, acct, pass);
    }
    //发送录音字节流 收到服务器返回网址
    public string HttpPostData(string url, Dictionary<string, string> dic)
    {
        var result = string.Empty;
        //#region 添加Post 参数  
        StringBuilder builder = new StringBuilder();
        int i = 0;
        foreach (var item in dic)
        {
            if (i > 0)
                builder.Append("&");
            builder.AppendFormat("{0}={1}", item.Key, item.Value);
            i++;
        }

        byte[] postData = Encoding.UTF8.GetBytes(builder.ToString());
        // 设置提交的相关参数 

        System.Net.HttpWebRequest request = System.Net.WebRequest.Create(url) as System.Net.HttpWebRequest;
        Encoding myEncoding = Encoding.UTF8;
        request.Method = "POST";
        request.KeepAlive = false;
        request.AllowAutoRedirect = true;
        request.ContentType = "application/x-www-form-urlencoded";
        request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR  3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)";
        request.ContentLength = postData.Length;
        request.Timeout = 3000;

        // 提交请求数据 
        System.IO.Stream outputStream = request.GetRequestStream();
        outputStream.Write(postData, 0, postData.Length);
        outputStream.Close();

        System.Net.HttpWebResponse response;
        Stream responseStream;
        StreamReader reader;
        string srcString;
        response = request.GetResponse() as System.Net.HttpWebResponse;
        responseStream = response.GetResponseStream();
        reader = new System.IO.StreamReader(responseStream, Encoding.GetEncoding("UTF-8"));
        srcString = reader.ReadToEnd();
        result = srcString;   //返回值赋值
        reader.Close();
        Debug.Log("获取HttpPostData服务器返回Version" + result);
        return result;
    }
}
